var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    imgs: "../../res/ddywc_banner.png",
    head: "../../res/imgs.png",
    add: true,
    zz: true
  },


  onLoad: function(options) {
    var diy_id = options.id;
    this.setData({
      diy_id:diy_id
    })
    this.getDiyShucai();
  },

  getDiyShucai:function(){
    var t = this;
    app.visit("huodong/getDiyShucai", {
      diy_id:t.data.diy_id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.setData({
          list:c.data
        })
      }else{
        app.showToast(c.msg)
      }
    })
  },

  headimage: function () {

    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9     
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        _this.setData({
          imgs: res.tempFilePaths
        })
      }
    })
  },

  addimage: function () {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res)
        wx.uploadFile({
          url: app.api_url + "/api/index/ups",
          filePath: res.tempFilePaths[0],
          name: "file",
          header: {
            Authorization: wx.getStorageSync("user_id"),
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(e) {
            console.error("上传回执", e);
            var a;
            0 == (a = "string" == typeof e.data ? JSON.parse(e.data) : e.data).code ? _this.setData({
              head: a.data.url,
              heads: a.data.img
            }) : wx.showToast({
              title: "上传失败"
            });
          },
          complete: function() {}
        });
      }
    })
  },
  add:function(){
    this.setData({
      add: false,
      zz: false
    })
  },
  close: function () {
    this.setData({
      add: true,
      zz: true
    })
  },

  shanchu:function(e){
    var id = e.currentTarget.dataset.id;
    var t = this;
    app.visit("huodong/shanchuDiySucai", {
      id:id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.getDiyShucai();
      }else{
        app.showToast(c.msg)
      }
    })
  },
  sucainame:function(e){
    this.setData({
      name:e.detail.value
    })
  },

  queding:function () {
    var t = this;
    var name = t.data.name;
    var heads = t.data.heads;
    app.visit("huodong/setSuCail", {
      diy_id:t.data.diy_id,
      name:name,
      heads:heads
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        wx.showToast({
          title: '添加成功',
          icon: 'none',
          success: function () {
            t.setData({
              add: true,
              zz: true
            })
            t.getDiyShucai();
          }
        })
      }else{
        app.showToast(c.msg)
      }
    })
  }

})